float4x4 World;
float4x4 View;
float4x4 Projection;

float4 modelColor;

// Diffuse
float3 lightPos;
float3 secondLightPos;

// Specular
float Shininess = 100;

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    
    // First lightsource.
    float3 WorldNormal : TEXCOORD0;
    float3 WorldLightdir : TEXCOORD1;
    float4 Color : COLOR0;
    
    // Second lightsource.
    float3 SecondWorldNormal : TEXCOORD2;
    float3 SecondWorldLightDir : TEXCOORD3;
    float4 SecondColor : COLOR1;

    float3 CamView : TEXCOORD4;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float3 worldNormal = normalize(mul(input.Normal, World)).xyz;
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    
    // First lightsource.
    output.WorldNormal = normalize(worldNormal);
    output.WorldLightdir = normalize(lightPos - worldPosition);
    
    // Second lightsource.
    output.SecondWorldNormal = normalize(worldNormal);
    output.SecondWorldLightDir = normalize(secondLightPos - worldPosition);

    output.Color = modelColor;
    output.SecondColor = float4(0, 0, 1, 1);
    
    output.CamView = normalize((viewPosition, 1.0) - worldPosition);

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{   
    float3 viewDir = normalize(input.CamView);

    // First lightsource.
    float3 ldir = normalize(input.WorldLightdir);
    float3 wnorm = normalize(input.WorldNormal);

    float diffuse = dot(ldir, wnorm);

    // Second ligtsource.
    float3 secondldir = normalize(input.SecondWorldLightDir);
    float3 secondwnorm = normalize(input.SecondWorldNormal);

    float secondDiffuse = dot(secondldir, secondwnorm);

    // Specular light.
    float3 ref = -reflect(ldir, wnorm);
    float4 specular = pow(saturate(dot(ref, viewDir)), Shininess);

    float3 secondRef = -reflect(secondldir, secondwnorm);
    float4 secondSpecular = pow(saturate(dot(secondRef, viewDir)), Shininess);

    float4 red = float4(1, 0, 0, 1);
    float4 green = float4(0, 1, 0, 1);
    //return float4(1, 1, 1, 1.0) * diffuse + input.Color;
    //return float4(1, 1, 1, 1.0) * secondDiffuse + input.SecondColor;
    //return float4(1, 1, 1, 1.0) * (diffuse + input.Color)  * (secondDiffuse + input.SecondColor);
    return input.Color + red * diffuse + (red * specular) + green * secondDiffuse + (green * secondSpecular);
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}
